import 'dart:io';

import 'package:flutter/material.dart';
import 'package:photo_view/photo_view.dart';
import 'package:photo_view/photo_view_gallery.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';

/// 图片预览页面
class PhotoViewPage extends StatelessWidget {

  final List<String> imgUrls;
  final double minScale;
  final double maxScale;
  dynamic initialScale;
  PhotoViewPage({
    Key? key,
    required this.imgUrls,
    this.maxScale = 2,
    this.minScale = 0.05,
    this.initialScale ,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return PhotoViewGallery.builder(itemCount: imgUrls.length, builder: (context, index) {
      return PhotoViewGalleryPageOptions(
        imageProvider: getImageProvider(imgUrls[index]),
        maxScale: PhotoViewComputedScale.contained * maxScale,
        minScale: minScale,
        initialScale: initialScale ?? PhotoViewComputedScale.contained * 0.8,
        heroAttributes: const PhotoViewHeroAttributes(tag: ""),
      );
    });
  }

}

ImageProvider getImageProvider(String url) {
  if (url.startsWith('http')) {
    return CachedNetworkImageProvider(url);
  } else if (url.startsWith('assets')) {
    return AssetImage(url);
  } else {
    return FileImage(
      File(url),
    );
  }
}

